package com.cls.business.controller;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.cls.common.annotation.ControllerEndpoint;
import com.cls.common.utils.MyUtil;
import com.cls.common.entity.MyConstant;
import com.cls.common.controller.BaseController;
import com.cls.common.entity.QueryRequest;
import com.cls.common.entity.ResponseVO;
import com.github.pagehelper.PageInfo;
import com.cls.business.entity.EntrustTopic;
import com.cls.business.service.IEntrustTopicService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PathVariable;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 *  Controller
 *
 * @author slx
 * @date 2020-12-10 17:17:35
 */
@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
public class EntrustTopicController extends BaseController {

    private final IEntrustTopicService entrustTopicService;

    /**
    * 列表页面
    **/
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/entrustTopic")
    public String entrustTopicIndex(){
        return MyUtil.view("business/entrustTopic/entrustTopic");
    }

    /**
    * 新增页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/entrustTopic/entrustTopicAdd")
    public String entrustTopicAdd(Model model){
        return MyUtil.view("business/entrustTopic/entrustTopicAdd");
    }

    /**
    * 修改页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/entrustTopic/entrustTopicUpdate/{topicId}")
    public String entrustTopicUpdate(@PathVariable Long topicId, Model model){
        EntrustTopic entrustTopic = entrustTopicService.getById(topicId);
        model.addAttribute("entrustTopic",entrustTopic);
        return MyUtil.view("business/entrustTopic/entrustTopicUpdate");
    }



    /**
    * 详情页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/entrustTopic/entrustTopicDetail/{topicId}")
    public String entrustTopicDetail(@PathVariable Long topicId, Model model){
        EntrustTopic entrustTopic = entrustTopicService.getById(topicId);
        model.addAttribute("entrustTopic",entrustTopic);
        return MyUtil.view("business/entrustTopic/entrustTopicDetail");
    }

    @GetMapping("entrustTopic")
    @ResponseBody
    @RequiresPermissions("entrustTopic:list")
    public ResponseVO getAllEntrustTopics(EntrustTopic entrustTopic) {
        return ResponseVO.success(entrustTopicService.findEntrustTopics(entrustTopic));
    }

    @GetMapping("entrustTopic/list")
    @ResponseBody
    @RequiresPermissions("entrustTopic:list")
    public ResponseVO entrustTopicList(QueryRequest request, EntrustTopic entrustTopic) {
        PageInfo<EntrustTopic> pageInfo =  this.entrustTopicService.findEntrustTopics(request, entrustTopic);
        return ResponseVO.success(pageInfo);
    }

    @ControllerEndpoint(operation = "新增EntrustTopic", exceptionMessage = "新增EntrustTopic失败")
    @PostMapping("entrustTopic")
    @ResponseBody
    @RequiresPermissions("entrustTopic:add")
    public ResponseVO addEntrustTopic(@Valid EntrustTopic entrustTopic) {
        this.entrustTopicService.createEntrustTopic(entrustTopic);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "多个删除EntrustTopic", exceptionMessage = "删除EntrustTopic失败")
    @PostMapping("entrustTopic/deletes/{ids}")
    @ResponseBody
    @RequiresPermissions("entrustTopic:delete")
    public ResponseVO deleteEntrustTopics(@PathVariable String ids) {
        String[] idArry = ids.split(StringPool.COMMA);
        this.entrustTopicService.deleteEntrustTopics(idArry);
        return ResponseVO.success();
    }


    @ControllerEndpoint(operation = "删除单个EntrustTopic", exceptionMessage = "删除EntrustTopic失败")
    @PostMapping("entrustTopic/delete/{topicId}")
    @ResponseBody
    @RequiresPermissions("entrustTopic:delete")
    public ResponseVO deleteEntrustTopic(@PathVariable Long topicId) {
        this.entrustTopicService.removeById(topicId);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改EntrustTopic", exceptionMessage = "修改EntrustTopic失败")
    @PostMapping("entrustTopic/update")
    @ResponseBody
    @RequiresPermissions("entrustTopic:update")
    public ResponseVO updateEntrustTopic(EntrustTopic entrustTopic) {
        this.entrustTopicService.updateEntrustTopic(entrustTopic);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改EntrustTopic", exceptionMessage = "导出Excel失败")
    @PostMapping("entrustTopic/excel")
    @ResponseBody
    @RequiresPermissions("entrustTopic:export")
    public void export(QueryRequest queryRequest, EntrustTopic entrustTopic, HttpServletResponse response) {
        List<EntrustTopic> entrustTopics = this.entrustTopicService.findEntrustTopics(queryRequest, entrustTopic).getList();
        ExcelKit.$Export(EntrustTopic.class, response).downXlsx(entrustTopics, false);
    }
}
